home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_std / character.e < prev    next >
Text File  |  1998-12-22  |  7KB  |  307 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. expanded class CHARACTER
  13. --
  14. -- Note: An Eiffel CHARACTER is mapped as a C char or as a Java Byte.
  15. --
  16.  
  17. inherit 
  18.    CHARACTER_REF
  19.       redefine
  20.      infix "<", infix "<=", infix ">", infix ">=", compare, 
  21.      code, to_lower, to_upper, out_in_tagged_out_memory, 
  22.      fill_tagged_out_memory, hash_code
  23.       end;
  24.  
  25. feature 
  26.  
  27.    to_integer: INTEGER is
  28.      -- Sign-extended conversion.
  29.       external "SmallEiffel" 
  30.       end;
  31.  
  32.    code: INTEGER is
  33.      -- ASCII code of Current.
  34.      -- No Sign-extended conversion.
  35.       external "SmallEiffel" 
  36.       ensure
  37.      Result >= 0
  38.       end;
  39.  
  40.    infix "<" (other: CHARACTER): BOOLEAN is
  41.      -- Comparison using `code'. 
  42.       do
  43.      Result := code < other.code;
  44.       end;
  45.    
  46.    infix "<="(other: CHARACTER): BOOLEAN is
  47.      -- Comparison using `code'. 
  48.       do
  49.      Result := code <= other.code;
  50.       end;
  51.    
  52.    infix ">"(other: CHARACTER): BOOLEAN is
  53.      -- Comparison using `code'. 
  54.       do
  55.      Result := code > other.code;
  56.       end;
  57.    
  58.    infix ">="(other: CHARACTER): BOOLEAN is
  59.      -- Comparison using `code'. 
  60.       do
  61.      Result := code >= other.code;
  62.       end;
  63.    
  64.    compare (other: CHARACTER): INTEGER is
  65.       do
  66.      Result := code - other.code
  67.       end;
  68.    
  69.    value: INTEGER is
  70.      -- Gives the value of a digit.
  71.       require
  72.      is_digit
  73.       do
  74.      Result := code - 48;
  75.       ensure
  76.      0 <= Result and Result <= 9;
  77.      definition: Result = code - 48;
  78.       end; -- value
  79.  
  80.    same_as(other: CHARACTER): BOOLEAN is
  81.      -- Case insensitive comparison.
  82.      -- No difference between upper/lower case letters.
  83.       do
  84.      if Current = other then
  85.         Result := true;
  86.      else
  87.         inspect
  88.            code
  89.         when 65 .. 90 then
  90.            Result := code = other.code - 32;
  91.         when 97 .. 122 then
  92.            Result := code = other.code + 32;
  93.         else
  94.         end;
  95.      end;
  96.       ensure
  97.      Result implies to_lower = other or to_upper = other;
  98.       end ;
  99.    
  100.    to_upper: CHARACTER is
  101.      -- Conversion to the corresponding upper case.
  102.       do
  103.      if code < 97 then
  104.         Result := Current;
  105.      elseif code > 122 then
  106.         Result := Current;
  107.      else
  108.         Result := (code - 32).to_character;
  109.      end;
  110.       end;
  111.    
  112.    to_lower: CHARACTER is
  113.      -- Conversion to the corresponding lower case.
  114.       do
  115.      if code < 65 then
  116.         Result := Current;
  117.      elseif code > 90 then
  118.         Result := Current;
  119.      else
  120.         Result := (code + 32).to_character;
  121.      end;
  122.       end;
  123.    
  124.    is_letter: BOOLEAN is
  125.       do
  126.      inspect 
  127.         Current
  128.      when 'A'..'Z','a'..'z' then
  129.         Result := true;
  130.      else
  131.      end;
  132.       ensure
  133.      Result = ('A' <= Current and Current <= 'Z') or 
  134.         ('a' <= Current and Current <= 'z');
  135.       end;
  136.    
  137.    is_digit: BOOLEAN is
  138.      -- Belongs to '0'..'9'.
  139.       do
  140.      inspect 
  141.         Current
  142.      when '0'..'9' then
  143.         Result := true;
  144.      else
  145.      end;
  146.       ensure
  147.     Result implies ('0' <= Current and Current <= '9');
  148.       end;
  149.    
  150.    is_lower: BOOLEAN is 
  151.      -- Is `item' lowercase?
  152.       do  
  153.          inspect 
  154.             Current
  155.          when 'a'..'z' then 
  156.                Result := true;
  157.          else 
  158.          end; 
  159.       end; 
  160.    
  161.    is_upper: BOOLEAN is 
  162.      -- Is `item' uppercase?
  163.       do  
  164.          inspect 
  165.             Current
  166.          when 'A'..'Z' then 
  167.                Result := true;
  168.          else 
  169.          end; 
  170.       end; 
  171.    
  172.    is_separator: BOOLEAN is
  173.      -- True when character is a separator.
  174.       do
  175.      inspect
  176.         Current
  177.      when ' ','%T','%N','%R','%U' then
  178.         Result := true;
  179.      else
  180.      end;
  181.       end;
  182.    
  183.    is_letter_or_digit: BOOLEAN is 
  184.       -- Is character a letter or digit ?
  185.       do  
  186.          Result := is_letter or is_digit;
  187.       ensure 
  188.          valid: Result = (is_letter or is_digit); 
  189.       end; 
  190.  
  191.    is_hex_digit: BOOLEAN is 
  192.      -- Is character one of "0123456789abcdefABCDEF" ?
  193.       do  
  194.          if is_digit then
  195.         Result := true;
  196.      elseif Current >= 'a' then
  197.         Result := Current <= 'f';
  198.      elseif Current >= 'A' then
  199.         Result := Current <= 'F';
  200.      end;
  201.       ensure
  202.      Result =  ("0123456789abcdefABCDEF").has(Current)
  203.       end; 
  204.  
  205.    is_ascii: BOOLEAN is 
  206.      -- Is character a 8-bit ASCII character?
  207.       do  
  208.          inspect 
  209.             Current
  210.          when '%U'..'%/255/' then 
  211.         Result := true;
  212.          else 
  213.          end; 
  214.       end; 
  215.  
  216.    is_bit: BOOLEAN is
  217.      -- True for `0' and `1'.
  218.       do
  219.      inspect
  220.         Current
  221.      when '0','1' then
  222.         Result := true;
  223.      else
  224.      end;
  225.       end;
  226.  
  227.    next: CHARACTER is
  228.      -- Give the next character (the following `code');
  229.       do
  230.      Result := (code + 1).to_character;
  231.       end;
  232.  
  233.    previous: CHARACTER is
  234.      -- Give the next character (the following `code');
  235.       require
  236.      code > 0
  237.       do
  238.      Result := (code - 1).to_character;
  239.       end;
  240.  
  241. feature -- Conversions :
  242.  
  243.    to_bit: BIT Character_bits is
  244.       external "SmallEiffel"
  245.       end;
  246.  
  247.    to_octal: INTEGER is
  248.       do
  249.      Result := code.to_octal;
  250.       end;
  251.  
  252.    to_hexadecimal: STRING is
  253.      -- Create a new STRING giving the `code' in hexadecimal.
  254.      -- For example :
  255.      --    (255).to_character.to_hexadecimal gives "FF".
  256.      -- Note: see `to_hexadecimal_in' to save memory.
  257.       do
  258.      !!Result.make(2);
  259.      to_hexadecimal_in(Result);
  260.       ensure
  261.      Result.count = 2
  262.       end;
  263.  
  264.    to_hexadecimal_in(str: STRING) is
  265.      -- Append the equivalent of `to_hexadecimal' at the end of 
  266.      -- `str'. Thus you can save memory because no other
  267.      -- STRING is allocate for the job.
  268.       local
  269.      c: CHARACTER;
  270.       do
  271.      c := ((to_bit and 11110000B) @>> 4).to_character;
  272.      inspect
  273.         c.code
  274.      when 0 .. 9 then
  275.         str.extend((('0').code + c.code).to_character);
  276.      else
  277.         str.extend(((('A').code - 10) + c.code).to_character);
  278.      end;
  279.      c := (to_bit and 00001111B).to_character;
  280.      inspect
  281.         c.code
  282.      when 0 .. 9 then
  283.         str.extend((('0').code + c.code).to_character);
  284.      else
  285.         str.extend(((('A').code - 10) + c.code).to_character);
  286.      end;
  287.       ensure
  288.      str.count = 2 + old str.count
  289.       end;
  290.  
  291. feature -- Object Printing :
  292.  
  293.    out_in_tagged_out_memory, fill_tagged_out_memory is
  294.       do
  295.      tagged_out_memory.extend(Current);
  296.       end;
  297.    
  298. feature -- Hashing :
  299.    
  300.    hash_code: INTEGER is
  301.       do
  302.      Result := code;
  303.       end;
  304.  
  305. end -- CHARACTER
  306.  
  307.